package com.hnkywl.sts.webservice.wly.impl;

import com.hnkywl.sts.dao.base.ClxxDao;
import com.hnkywl.sts.entity.base.Clxx;
import com.hnkywl.sts.service.base.WebserviceLogService;
import com.hnkywl.sts.webservice.wly.IPutMaterialInformation;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2016/6/22.
 * 接收材料信息
 */
@Service
public class PutMaterialInformationService implements IPutMaterialInformation, ApplicationContextAware {
    private ApplicationContext applicationContext;
    private ClxxDao clxxDao;
    private WebserviceLogService webserviceLogService;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        clxxDao = (ClxxDao) applicationContext.getBean("clxxDao");
        webserviceLogService = (WebserviceLogService) applicationContext.getBean("webserviceLogService");

    }

    /**
     * 接受材料信息保存到相应的数据表中
     *
     * @param json
     * @return
     */
    @Override
    public synchronized String PutMaterialInformation(String json) {
        String result;
        String exception = null;
        try {
            //1、查出所有材料信息
            this.checkClxxJson(json);//先校验通过
            this.CreateClxx(json);//保存材料信息
            result = "{\"code\":\"0\",\"message\":\"保存材料信息成功!\"}";
        } catch (Exception e) {
            exception = e.getMessage();
            result = "{\"code\":\"1\",\"message\":\"" + e.getMessage() + "\"}";
        }
        try {
            JSONArray jsonArray = new JSONArray(json);
            webserviceLogService.saveServerLog(
                    PutMaterialInformationService.class.getName(),
                    "PutMaterialInformationService",
                    jsonArray.toString(),
                    "返回结果：" + result,
                    exception,
                    ""
            );
        } catch (Exception e) {
            webserviceLogService.saveServerLog(
                    PutMaterialInformationService.class.getName(),
                    "PutMaterialInformationService",
                    json,
                    "返回结果：" + result,
                    exception,
                    ""
            );
        }
        return result;
    }

    //校验json
    public void checkClxxJson(String json) throws Exception {



        if (json == null || json.isEmpty()) {
            throw new RuntimeException("json为空!");
        }

        JSONArray jsonArray = new JSONArray(json);
        int iSize = jsonArray.length();
        if (iSize == 0) {
            throw new RuntimeException("json数组中为空!");
        }

        for (int i = 0; i < iSize; i++) {
            JSONObject jsonObj = jsonArray.getJSONObject(i);
            String clh = (String) jsonObj.get("matId");
            if (clh == null || "".equals(clh)) {
                throw new RuntimeException("材料号为空!");
            }

            if (!"null".equals(clh) && !clh.isEmpty()) {

                Boolean flag_serialNumber = clxxDao.checkClh(clh);
                if (!flag_serialNumber) {
                    throw new RuntimeException("材料号重复!请勿重复保存!");
                }

                String ccrq = (String) jsonObj.get("leaveFacDate");//出厂日期
                if (ccrq == null || "".equals(ccrq)) {
                    throw new RuntimeException("出厂日期为空!");
                } else {
                    try {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        sdf.parse(ccrq);
                    } catch (Exception e) {
                        throw new RuntimeException("出厂日期时间格式错误!");
                    }
                }
            }
        }
    }

    //保存材料信息
    public void CreateClxx(String json) throws Exception {
        JSONArray jsonArray = new JSONArray(json);
        int iSize = jsonArray.length();

        for (int i = 0; i < iSize; i++) {
            JSONObject jsonObj = jsonArray.getJSONObject(i);
            String clh = (String) jsonObj.get("matId");
            if (!"null".equals(clh) && !clh.isEmpty()) {
                Clxx clxx = new Clxx();
//                clxx.setCreateUser();
                clxx.setLastUpdated(new Date());
                clxx.setJd((String) jsonObj.get("factoryId"));
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String ccrq1 = (String) jsonObj.get("leaveFacDate");
                clxx.setCcrq(sdf.parse(ccrq1));
                clxx.setCcch((String) jsonObj.get("facVehicleId"));
                clxx.setXshth((String) jsonObj.get("saleOrderItemNum"));
                clxx.setCljz(Double.parseDouble((String) jsonObj.get("actNetWeight")));
                clxx.setClmz(Double.parseDouble((String) jsonObj.get("grossWeight")));
                clxx.setCljs(Integer.parseInt((String) jsonObj.get("actCount")));
                clxx.setCkmdh((String) jsonObj.get("putoutStackingId"));
                clxx.setXsgsmc((String) jsonObj.get("saleCompanyName"));
                clxx.setDyshyhmc((String) jsonObj.get("cnsgAddress"));
                clxx.setDeshyhmc((String) jsonObj.get("consignName1"));
                clxx.setDzmc((String) jsonObj.get("destSpotName"));
                clxx.setJsyhmc((String) jsonObj.get("settleUserName"));
                clxx.setZzyhmc((String) jsonObj.get("finUserName"));
                clxx.setGg((String) jsonObj.get("specDesc"));
                clxx.setPh((String) jsonObj.get("shopsign"));
                clxx.setPzmc((String) jsonObj.get("productName"));
                clxx.setCkzt((String) jsonObj.get("status"));
                clxx.setCjsj((String) jsonObj.get("recCreateTime"));
                clxx.setClh((String) jsonObj.get("matId"));
                clxx.setZt("0");
                clxx.setBz(" ");

                clxxDao.save(clxx);

            }
        }

    }
}
